<template>
  <div class="recommend" ref="recommend">
    <scroll  ref="scroll" class="recommend-content" :dataA="discList">
		<div> 
			 <!-- 轮播图插件  scroll组件使用时 记得要嵌套一层父元素 -->
			<div ref="sliderWrapper" class="slider-wrapper" v-if="recommends.length">
				<slider>
					<div v-for="item in recommends" :key="item.id" >
						<a :href="item.linkUrl">
							<img class="needsclick" :src="item.picUrl" alt="">
						</a>
					</div>
				</slider>	
			</div>
			<!-- 热门歌单推荐 -->
			<div class="recommend-list">
					<div class="list-title">热门歌单推荐</div>
						<ul>
							<li @click="selectItem(item)" v-for="(item,index) in discList" class="item"  :key="index">
								<div class="icon">
									<img @load="loadimgage" width="60" height="60" v-lazy="item.pic">
								</div>
								<div class="text">
									<h2 class="name" v-html="item.creator"></h2>
									<p class="desc" v-html="item.name"></p>
								</div>
							</li>
						</ul>
				</div>
			
	    </div>
		<!-- loading 延迟加载 -->
		<div class="loading-container"  v-show="!discList.length" >
			<loading></loading> 
		</div>
    </scroll>
	<router-view></router-view>
  </div>
</template>
<!-- 
LOAD事件是加载事件，当你窗体加载成功后就会触发的事件，比如说你想初始化某些属性就可以在LOAD事件内完成。
LOAD你可以看做是被动事件，不管你用不用，它都存在着。如果你不想用，事件里则不需要写任何代码。 
-->
<script>
import BScroll from 'better-scroll'
import loading from 'base/loading/loading';
import {getRecommend, getDiscList} from 'api/recommend'; //首页请求方式
import slider from 'base/slider/slider';//轮播图组件
import {ERR_OK,ERR_GO} from 'api/config'
import scroll from 'base/scroll/scroll'
import axios from 'axios'
import {playlistMixin} from 'common/js/mixin'
import {mapMutations} from 'vuex'
import VConsole from 'vconsole/dist/vconsole.min.js' //import vconsole
let vConsole = new VConsole() // 初始化

export default {
  mixins: [playlistMixin],
  name: 'recommend',
  data(){
	  return{
		recommends:[],
		discList:[]
	  }
  },
  components: { 
    loading,
	slider,
	scroll
  },
  created () {
	this._getRecommend() //所有数据 轮播图
    this._getDiscList() //获取歌单列表 分类歌单
  },
  methods: {
	handlePlaylist(playlist) {
	  const bottom = playlist.length > 0 ? '60px' : ''
	  this.$refs.recommend.style.bottom = bottom
	  this.$refs.scroll.refresh()
	},
	_getRecommend() {//所有数据
       getRecommend().then((res) => { //封装了获取首页轮播图的请求方式 
       if (res.code === ERR_OK) {
            this.recommends = res.data.slider
          }
        })
    },
	selectItem(item){
		this.$router.push({
			path: `/recommend/${item.id}`
		})
		this.setDisc(item) //vuex 跟单数据
	},
	_getDiscList() {//获取歌单下拉列表链接
		let url='https://api.bzqll.com/music/tencent/hotSongList?key=579621905&categoryId=10000000&sortId=3&limit=60'
		axios.get(url)
		.then(this.gethomrinfosuc)
	},
	gethomrinfosuc(res){ //重新获取数据
		let	resdata=res.data.data;
	    if (res.data.code === ERR_GO) {
			this.discList =resdata
		}else{
			console.log('请求错误http://api.bzqll.com/')
		}
	},
	loadimgage(){ //图片和列表显示时间不同的解决方案
		if(!this.checkLoaded){//这个方法只会执行一次 
		  this.$refs.scroll.refresh()
		  this.checkLoaded=true;
		}
	},
	...mapMutations({
		setDisc: 'SET_DISC'
	})
}
}
</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  @import "~common/stylus/variable"

  .recommend
    position: fixed
    width: 100%
    top: 88px
    bottom: 0
    .recommend-content
      height: 100%
      overflow: hidden
      .slider-wrapper
        position: relative
        width: 100%
        overflow: hidden
      .recommend-list
        .list-title
          height: 65px
          line-height: 65px
          text-align: center
          font-size: $font-size-medium
          color: $color-theme
        .item
          display: flex
          box-sizing: border-box
          align-items: center
          padding: 0 20px 20px 20px
          .icon
            flex: 0 0 60px
            width: 60px
            padding-right: 20px
          .text
            display: flex
            flex-direction: column
            justify-content: center
            flex: 1
            line-height: 20px
            overflow: hidden
            font-size: $font-size-medium
            .name
              margin-bottom: 10px
              color: $color-text
            .desc
              color: $color-text-d
      .loading-container
        position: absolute
        width: 100%
        top: 50%
        transform: translateY(-50%)
</style>